////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   SimpleBaseActionDispatcher.java
// AUTHOR           :   ric.yik
// DATE CREATE      :   Mar 8, 2006
// DESCRIPTION      :   
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// Mar 8, 2006  ric.yik         1.0                 source is created.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.common.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.framework.SimpleConstants;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

/**
 * The abstract class for ActionDispatcher. This class is extracted from 
 * ActionMethodDispatcher for allowing extension of pageEvent dispatching mechanism.
 * 
 * <pre>
 * In contract to the original ActionMethodDispatcher, a new property, 
 * namly nextDispatcher, is added. This forms a chain of event dispatching classes 
 * and allow the invocation of the next dispatcher if the current dispatcher don't 
 * know how to dispatch a given pageEvent.
 * 
 * First used in: Anco Vendor Module (SimpleMaintMultiRowAction)
 * </pre>
 * 
 * @author Ric YIK
 */
public abstract class SimpleBaseActionDispatcher {

    private Action action;

    private ActionForward defaultActionForward;

    private SimpleBaseActionDispatcher nextDispatcher;

    private boolean processStatus = false;

    private String status;

    public Action getAction() {
        return action;
    }

    public SimpleBaseActionDispatcher setAction(Action action) {
        this.action = action;
        return this;
    }

    public ActionForward getDefaultActionForward() {
        return defaultActionForward;
    }

    public SimpleBaseActionDispatcher setDefaultActionForward(ActionForward defaultActionForward) {
        this.defaultActionForward = defaultActionForward;
        return this;
    }

    public SimpleBaseActionDispatcher getNextDispatcher() {
        return nextDispatcher;
    }

    public SimpleBaseActionDispatcher setNextDispatcher(SimpleBaseActionDispatcher nextDispatcher) {
        this.nextDispatcher = nextDispatcher;
        return this;
    }

    public boolean isProcessStatus() {
        return processStatus;
    }

    public SimpleBaseActionDispatcher setProcessStatus(boolean processStatus) {
        this.processStatus = processStatus;
        return this;
    }

    public String getStatus() {
        return status;
    }

    public SimpleBaseActionDispatcher setStatus(String status) {
        this.status = status;
        return this;
    }

    public SimpleBaseActionDispatcher inheritsSimpleBaseActionDispatcher(SimpleBaseActionDispatcher baseDispatcher) {
        if (this.getAction() == null) this.setAction(baseDispatcher.getAction());
        if (this.getDefaultActionForward() == null) this.setDefaultActionForward(baseDispatcher.getDefaultActionForward());
        if (baseDispatcher.isProcessStatus()) {
            this.setProcessStatus(baseDispatcher.isProcessStatus());
            this.setStatus(baseDispatcher.getStatus());
        }
        return this;
    }

    public abstract ActionForward processAction(String pageEvent, ActionMapping mapping, ActionForm formIn, 
            HttpServletRequest request, HttpServletResponse response, ActionErrors errors);

    public ActionForward processWithNextDispatcher(String pageEvent, ActionMapping mapping, ActionForm formIn, 
            HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
        ActionForward actionForward = this.getDefaultActionForward();
        if (this.getNextDispatcher() == null) {
            if (!SimpleConstants.ACTION_DO_NOTHING.equals(pageEvent)) {
                Logger.debug(this.getAction(), "No action method for the action " + pageEvent);
            }
        } else {
            actionForward = this.getNextDispatcher()
                .inheritsSimpleBaseActionDispatcher(this).processAction(pageEvent, mapping, formIn, request, response, errors);
        }
        return actionForward;
    }

}
